package com.wz.wzbubbleai.database;

/**
 * Created by wangbin on 18-1-16.
 */


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wz.wzbubbleai.apdater.Item.BaseListItem;
import com.wz.wzbubbleai.apdater.Item.BubblePlugItem;
import com.wz.wzbubbleai.apdater.Item.BubbleTextItem;
import com.wz.wzbubbleai.apdater.Item.UserTextItem;
import com.wz.wzbubbleai.enity.ChatModel;
import com.wz.wzbubbleai.utils.ConvertUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *  ChatDataDao  数据库操作类
 *
 */
public class ChatDataDao {

    private ChatDataBaseHelper mDBHelper;

    /**
     * dao类需要实例化数据库Help类,只有得到帮助类的对象我们才可以实例化 SQLiteDatabase
     * @param context
     */
    public ChatDataDao(Context context) {
        mDBHelper = new ChatDataBaseHelper(context);
    }

    /**
     * 增加数据
     * @param chatData
     * @return
     */
    public long addDate(ChatModel chatData){
        SQLiteDatabase sqLiteDatabase =  mDBHelper.getWritableDatabase();

        ContentValues contentValues=new ContentValues();

        switch (chatData.getChatType()){
            case BaseListItem.TYPE_USER_TEXT:{
                contentValues.put(NewChatTable.CHAT_TYPE,chatData.getChatType());
                contentValues.put(NewChatTable.CHAT_TEXT, chatData.getChatText());
            }
            break;
            case BaseListItem.TYPE_BUBBLE_TEXT:{
                contentValues.put(NewChatTable.CHAT_TYPE,chatData.getChatType());
                contentValues.put(NewChatTable.CHAT_TEXT, chatData.getChatText());
            }
            break;
            case BaseListItem.TYPE_BUBBLE_PLUG:{
                contentValues.put(NewChatTable.CHAT_TYPE,chatData.getChatType());
                contentValues.put(NewChatTable.CHAT_TEXT, chatData.getChatText());
                contentValues.put(NewChatTable.CHAT_PLUG_NAME, chatData.getChatPlugName());
                contentValues.put(NewChatTable.CHAT_PLUG_VERSION,chatData.getChatPlugVersion());
                contentValues.put(NewChatTable.CHAT_DESCRIPTION,chatData.getChatDescription());
            }
            break;
        }



        long rowid = sqLiteDatabase.insert(NewChatTable.TBL_NAME,null,contentValues);

        sqLiteDatabase.close();
        return rowid;
    }


    /**
     * 删除记录
     * @param name
     * @return
     */
    public int deleteDate(String name){
        SQLiteDatabase sqLiteDatabase = mDBHelper.getWritableDatabase();
        int deleteResult = sqLiteDatabase.delete(NewChatTable.TBL_NAME,null ,null );
        sqLiteDatabase.close();
        return deleteResult;
    }

    /**
     * 修改记录
     * @param name
     * @param newPhone
     * @return
     */
    public int updateData(String name,String newPhone){
        SQLiteDatabase sqLiteDatabase = mDBHelper.getWritableDatabase();
        ContentValues contentValues =new ContentValues();
        contentValues.put(NewChatTable.TBL_NAME, newPhone);
        int updateResult = sqLiteDatabase.update(NewChatTable.TBL_NAME, contentValues, "name=?", new String[]{name});
        sqLiteDatabase.close();
        return updateResult;
    }

    /**
     * 查询
     * @param name
     * @return
     */
    public List<BaseListItem> getChatDate(String name){
        List<BaseListItem> itemList = new ArrayList<BaseListItem>();

        SQLiteDatabase readableDatabase = mDBHelper.getReadableDatabase();
        // 查询比较特别,涉及到 cursor
        Cursor cursor = readableDatabase.query(NewChatTable.TBL_NAME, null, null,
                null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                // 遍历Cursor对象
                int type = cursor.getInt(cursor. getColumnIndex(NewChatTable.CHAT_TYPE));

                String text = cursor.getString(cursor.getColumnIndex(NewChatTable.CHAT_TEXT));

                String plugName = cursor.getString(cursor.getColumnIndex(NewChatTable.CHAT_PLUG_NAME));

                int version = cursor.getInt(cursor.getColumnIndex(NewChatTable.CHAT_PLUG_VERSION));

                String dsc = cursor.getString(cursor.getColumnIndex(NewChatTable.CHAT_DESCRIPTION));

                itemList.add(ConvertUtils.DataToBaseItem(new ChatModel(type,text,plugName,version,dsc)));
            } while (cursor.moveToNext());
        };
        cursor.close();
        readableDatabase.close();
        return itemList;
    }
}
